package com.b2b.modules.fm.assets;

import com.b2b.common.typewrap.Dto;
import com.b2b.common.typewrap.Dtos;
import com.b2b.common.utils.CacheUtils;
import com.b2b.common.utils.SpringContextHolder;
import com.b2b.common.utils.StringUtils;
import com.b2b.modules.fm.dao.base.FmBrandDao;
import com.b2b.modules.fm.dao.base.FmEquipcatalogDao;
import com.b2b.modules.fm.dao.base.FmEquipmentDao;
import com.b2b.modules.fm.dao.base.FmStoreDao;
import com.b2b.modules.fm.entity.base.FmBrand;
import com.b2b.modules.fm.entity.base.FmEquipcatalog;
import com.b2b.modules.fm.entity.base.FmEquipment;
import com.b2b.modules.fm.entity.base.FmStore;
import com.b2b.modules.sys.entity.Log;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.sun.org.apache.bcel.internal.generic.FMUL;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.swing.*;
import java.util.List;
import java.util.Map;

/**
 * 全家监控模块 工具类
 * Created by Soldier lee on 2016-11-20.
 */
public class FmUtils {
    private static Logger logger = LoggerFactory.getLogger(FmUtils.class);
    public static final String CACHE_FM_BRAND_LIST = "fmBrandList";//品牌缓存
    public static final String CACHE_FM_EQUIPMENT_CATALOG_MAP="fmEquipmentCatalogMap";//设备分类名目 小类
    /**
     * 根据门店编号获取门店名称
     * @param storeNo
     * @return
     */
    public static FmStore getFmStore(String storeNo){
        FmStoreDao fmStoreDao = SpringContextHolder.getBean(FmStoreDao.class);
        return  fmStoreDao.findUniqueByProperty("sto002",storeNo);
    }

    /**
     * 获取所有品牌列表
     * @return
     */
    public static List<FmBrand> getFmBrands(){
        List<FmBrand> fmBrandList  =  (List<FmBrand>) CacheUtils.get(CACHE_FM_BRAND_LIST);
        if (fmBrandList == null){
            FmBrandDao fmBrandDao = SpringContextHolder.getBean(FmBrandDao.class);
            fmBrandList = fmBrandDao.findAllList(new FmBrand());
            CacheUtils.put(CACHE_FM_BRAND_LIST,fmBrandList);
        }
        return fmBrandList;

    }
    public static List<FmEquipcatalog> getFmEquipCatalogs(String type){
        Map<String, List<FmEquipcatalog>> fmEquipcatalogMap = (Map<String, List<FmEquipcatalog>>)CacheUtils.get(CACHE_FM_EQUIPMENT_CATALOG_MAP);
        if (fmEquipcatalogMap==null){
            fmEquipcatalogMap = Maps.newHashMap();
            FmEquipcatalogDao fmEquipcatalogDao = SpringContextHolder.getBean(FmEquipcatalogDao.class);
            for (FmEquipcatalog catalog : fmEquipcatalogDao.findAllList(new FmEquipcatalog())){
                List<FmEquipcatalog> catalogList = fmEquipcatalogMap.get(catalog.getEqc002());
                if (catalogList != null){
                    catalogList.add(catalog);
                }else{
                    fmEquipcatalogMap.put(catalog.getEqc002(), Lists.newArrayList(catalog));
                }
            }
            CacheUtils.put(CACHE_FM_EQUIPMENT_CATALOG_MAP, fmEquipcatalogMap);
        }
        List<FmEquipcatalog> catalogList = fmEquipcatalogMap.get(type);
        if (catalogList == null){
            catalogList = Lists.newArrayList();
        }
        return catalogList;
    }

    /**
     * 获取设备温度参考线
     * 查找逻辑为：
     * 1.对应设备记录中是否设置，如果设置则取对应的参数；
     * 2.未在设备记录中查询到，则根据设备所属小类进行获取全局的参数配置
     * @param params {mno: 主机号   ,dno: 设备号}
     * @return {baseTem:标准温度,upTem:警示上线，dwonTem:警示下线，customTem：温控纠偏}
     */
    public static Dto getTemReferenceLineData(Dto params){
        Dto outDto = Dtos.newOutDto();
        FmEquipmentDao fmEquipmentDao = SpringContextHolder.getBean(FmEquipmentDao.class);
        Dto equipment = fmEquipmentDao.getTemReferenceLineData(params);
        double baseTem = 0;
        double allowTem = 0;
        double customTem = 0;
        if (StringUtils.isNotEmpty(equipment)&&equipment.getDouble("equ010")!=null){
            baseTem = equipment.getDouble("equ010");
            allowTem = equipment.getDouble("equ011");
            customTem = equipment.getDouble("equ012")==null?0:equipment.getDouble("equ012");
        }else{
            //从设备类型表中进行搜索
            FmEquipcatalogDao fmEquipcatalogDao = SpringContextHolder.getBean(FmEquipcatalogDao.class);
            FmEquipcatalog fmEquipcatalog = fmEquipcatalogDao.get(equipment.getString("equ006"));
            if (StringUtils.isNotEmpty(fmEquipcatalog)&&fmEquipcatalog.getEqc005()!=null){
                baseTem = fmEquipcatalog.getEqc005();
                allowTem = fmEquipcatalog.getEqc006();
                customTem = fmEquipcatalog.getEqc007()==null?0:fmEquipcatalog.getEqc007();

            }
        }
        outDto.put("baseTem",baseTem);
        outDto.put("upTem",baseTem+allowTem);
        outDto.put("dwonTem",baseTem-allowTem);
        outDto.put("customTem",customTem);

        return outDto;
    }


}
